<template>

</template>

<script>
  //接口
  import {
    invoiceType, //系统字典
    listDept, //部门列表
    listWarehouses, //仓库列表
    checkWarehouses, //仓库信息查询
  } from '../../api/checkin';
  //js方法
  import {
    getTreeData,
    getEcho
  } from "../../utils/checkin"
  //并行请求
  import axios from 'axios';
  //打印js
  import print from "print-js";
  //输入是否为数字效验
  import {
    isAccord
  } from "@/utils/cingsoft"

  export default {
    data() {

      return {
        // 遮罩层
        loading: true,
        //仓库代码
        whCode: "",
        //按钮显示开关 新增/查看/编辑
        btnStatus: "", //isShow
        //按钮是否禁用
        btnIsClick: false, //disShow
        //输入框是否可以编辑
        disabledInput: false, //disabledStatus
        // 进入表单模式，有参、无参
        fixedQuery: false,
        // 仓库业务类型是否可更改
        wmworkStatus: false,
        // 单价数量小数位数
        decimalData: {},
        //上一条下一条状态
        stripStatus: false,
        //查看code
        seeCode: "",
        //记录蓝红字
        recordBredVouch: "",
        //路由信息用于关闭页面
        routeMsg: {},
        //单据类型
        optionsInvoiceType: [],
        selectInvoiceType: "",
        //业务类型
        optionsworkType: [],
        selectworkType: "",
        workTypeInit: [],
        examineForm: {
          wmCode: "",
          check: ""
        },
        workTypeStatus: false,
        //业务部门
        optionsworkDept: [],
        selectworkDept: [],
        //业务状态
        optionsworkStatus: [],
        selectworkStatus: "",
        //客户
        optionscustomer: [],
        omStatus: false,
        omSearchStatus: false,
        //供方
        optionssupplier: [],
        poStatus: false,
        poSearchStatus: false,
        //外协厂家
        optionsoutsource: [],
        puStatus: false,
        puSearchStatus: false,
        //加工设备
        optionsproductionLine: [],
        prsStatus: false,
        deptId: "",
        // 导入信息
        optionsImportMaterial: [],
        ImportMaterialStatus: false,
        // 导入组件传值 仓库code/料品code/物料大类特殊属性
        ImportMaterialCode: {
          workType: "",
          warehouse: ""
        },
        //业务部门选择配置
        classProps: {
          children: "children",
          label: "deptName",
          value: "deptId",
        },
        //业务人员
        optionsworkStaff: [],
        selectworkStaff: "",
        //仓库
        optionswarehouse: [],
        selectwarehouse: "",
        warehouseStatus: false,
        //仓库对应物料分类信息
        wmSort: {
          sortTree: [],
          sortId: "",
          sortPrice: "2", //价格小数位数
          sortQuantity: "2", //数量小数位数
          sortAttrAlia: "型号规格", //别名
          sortAttrMerge: 0, //规格合并
        },
        sortArrId: [],
        // 物料特殊属性
        wmSpecail: [],
        totalAmount: 0.00,
        //业务部门列表
        deptList: [],
        rowIndex: "", //选择物料时的下标
        temporaryObj: [], //弹框物料选中值中转站
        // 物料数据
        basicfileList: [],
        //货位显示状态 是否启用货位
        locationStatus: true,
        // 货位信息
        optionslocation: [],
        isLocation: false, //货位组件状态
        whName: "",
        //单价状态
        priceStatus: true,
        // 下拉框开关
        saveIndex: null,
        // 物料查询参数
        queryParams: {
          code: "",
          sortRoot: "",
          type: true,
        },
        //添加物料
        addProduct: {
          invSortId: "", //物料分类
          invCode: "", //物料编码
          invName: "", //物料名称
          invAttribute: "", //型号规格
          unit: "", //主计量
          quantity: "", //采购数量
          price: "", //采购单价
          amount: "0.00", //采购金额
          recCode: "", //关联单号
          recCodeId: "", //关联流水
          location: "", //货位
          remarks: "", //备注
        },
        //是否显示空数据提示
        emptyStatus: false,
        // 展开收起/窗口高度
        screenHeight: "",
        isToggle: true,
        downTitle: "收起",
        boxHeight: "300px",
        tableHeight: "",
        selectDialog: false,
        //打印数据
        printData: {},
        //打印名称
        printName: "入库单",
        //打印组件状态
        printStatus: false,
        printKey: "",
        //打印部门
        printWorkDept: [],
        printArr: [],
        //打印数据长度
        printLength: ""
      }
    },
    props: {
      formInput: {
        type: Object,
        default () {
          return {}
        }
      },
      selectPrint: { //批量打印状态
        type: String,
        default: ""
      },
      BatchPrintData: { //批量打印对应数据
        type: Array,
        default () {
          return []
        }
      },
      wmCodeArr: { //批量打印对应数据
        type: Array,
        default () {
          return []
        }
      },
    },

    created() {
      this.fixedQuery = false;
      //初始化页面
      this.Initial();
      // console.log(this.BatchPrintData)
    },
    methods: {
      Initial() {

        // 按钮状态
        this.btnStatus = "seeStatus";
        if (this.formInput.bredVouch == 0) {
          this.getDic("wm_in_type").then(() => {

            this.getDictionaryType()
          })
        } else if (this.formInput.bredVouch == 1) {
          this.getDic("wm_reject_in").then(() => {
            this.getDictionaryType()
          })
        } else {
          this.getDic("wm_adjust_in").then(() => {
            this.getDictionaryType()
          })
        }
      },
      /* 字典类型 */
      getDic(type) {
        return new Promise((resolve, reject) => {
          invoiceType({
            dictType: type
          }).then(res => {
            this.optionsworkType = [];
            this.optionsworkType = res.rows;
            this.workTypeInit = res.rows;
            resolve()
          })
        })


      },

      /** 获取字典单据与业务信息 */
      getDictionaryType() {

        const _self = this;
        _self.loading = true;
        //并行请求
        axios.all([
          invoiceType({
            dictType: "wm_invoice_type"
          }),
          invoiceType({
            dictType: "sys_work_status"
          }),
          listDept(),
          listWarehouses({
            bredVouch: _self.formInput.bredVouch,
            invoiceType: _self.formInput.invoiceType,
            workType: _self.formInput.workType
          })
        ]).then(
          axios.spread(
            function(resIvType, resWorkStatus, resDept, resHouses) {
              if (resIvType.code == 200 && resDept.code == 200 && resHouses.code == 200) {
                _self.optionswarehouse = [];
                // 单据类型
                _self.optionsInvoiceType = resIvType.rows;
                // 业务部门
                let arr = _self.handleTree(resDept.data, "deptId");
                _self.optionsworkDept = getTreeData(arr)
                //仓库
                _self.optionswarehouse = resHouses.rows;
                //业务状态
                _self.optionsworkStatus = resWorkStatus.rows;

                // 业务状态
                _self.selectworkStatus = _self.formInput.workStatus;
                // 查看/编辑数据
                if (_self.btnStatus == 'seeStatus' && _self.formInput !== null || _self.btnStatus == 'updateStatus') {

                  //单据类型
                  _self.selectInvoiceType = _self.formInput.invoiceType;
                  //业务类型
                  _self.selectworkType = _self.formInput.workType;
                  //仓库
                  _self.selectwarehouse = _self.formInput.warehouse;
                  if (_self.formInput.workDept !== "") {
                    // 部门数据回显
                    _self.undataAddSee(_self.formInput.workDept);
                  }
                  // 业务人员
                  _self.selectworkStaff = _self.formInput.workStaff;
                  // 业务状态
                  _self.selectworkStatus = _self.formInput.workStatus;
                  //记录蓝红/黄字
                  _self.recordBredVouch = _self.formInput.bredVouch;
                  //匹配物料特殊属性物料信息导航名称/小数位数

                  _self.warehouseChange(_self.formInput.warehouse)
                  _self.workTypeStatus = true;

                }
              } else {
                _self.msgError("获取信息失败")
              }
            }
          )
        )

      },

      /** 仓库选择 */
      warehouseChange(value) {
        //仓库回显名称
        this.selectwarehouse = value;
        this.formInput.warehouse = value;
        this.WarehouseCheck(value).then(res => {
          this.handlePrint()
        })

      },
      WarehouseCheck(d) {
        return new Promise((resolve, reject) => {
          checkWarehouses(d).then(res => {
            if (res.code == 200) {
              //物料分类下单价小数点位数
              this.wmSort.sortPrice = res.data.invSorts[0].priceDigit;
              //物料分类下数量小数点位数
              this.wmSort.sortQuantity = res.data.invSorts[0].quantityDigit;
              //物料分类下规格别名
              this.wmSort.sortAttrAlia = res.data.invSorts[0].attrAlias || "型号规格";
              //物料分类下规格合并
              this.wmSort.sortAttrMerge = res.data.invSorts[0].attrMerge;
              //物料分类下特殊物料
              this.wmSpecail = res.data.invSorts[0].invIndividuals;
              //仓库名称
              this.whName = res.data.name;
              if (res.data.enableLocation == 1) {
                this.locationStatus = true;
              } else {
                this.locationStatus = false;
              }
              if (res.data.invSorts[0].nonePrice == "0") {
                this.priceStatus = true;
              } else {
                this.priceStatus = false;
              }
              if (this.btnStatus == 'seeStatus' || this.btnStatus ==
                'updateStatus') {
                this.formInput.wmInSalveList.forEach(item => {
                  if (item.price !== null) {
                    item.price = parseFloat(item.price).toFixed(res.data.invSorts[0].priceDigit);
                  }
                  if (item.quantity !== null) {
                    item.quantity = parseFloat(item.quantity).toFixed(res.data.invSorts[0].quantityDigit)
                  }
                  if (item.amount !== null) {
                    item.amount = parseFloat(item.amount).toFixed(2)
                  }
                })
              }
              // 业务类型选择根据仓库类型回显
              if (this.formInput.bredVouch == 0 || this.formInput.bredVouch == 1) {
                this.optionsworkType = [];
                let type = res.data.inType.split(",");
                type.forEach(id => {
                  let objType = this.workTypeInit.find(item => {
                    return item.dictValue == id;
                  })
                  this.optionsworkType.push(objType);
                })
              }

              this.loading = false;
              // 延迟显示物料信息优化体验
              setTimeout(() => {
                this.warehouseStatus = true;
              }, 50)

              resolve()
            }
          })
        })

      },

      /* 打印 */
      handlePrint() {
        let transferData = JSON.parse(JSON.stringify(this.formInput))
        //业务类型
        this.workTypeInit.forEach(obj => {
          if (obj.dictValue == transferData.workType) {
            transferData.workTypeTitle = obj.dictLabel;
            this.printName = transferData.workTypeTitle + "单"
          }
        });
        // 仓库
        let warehouse = this.optionswarehouse.find(obj => {
          return transferData.warehouse == obj.code
        })
        transferData.warehouse = warehouse.name;
        //部门
        transferData.workDept = this.printWorkDept.join("/");
        this.printData = transferData;
        let number = 1;
        this.printData.wmInSalveList.forEach(obj => {
          obj.id = number++
        });
        this.printData.printTime = this.parseTime(new Date);
        this.printStatus = true;

        // 批量打印
        this.printData.wmSort = this.wmSort;
        this.printData.wmSpecail = this.wmSpecail;
        this.printData.priceStatus = this.priceStatus;
        this.printData.locationStatus = this.locationStatus;
        this.printData.title = transferData.workTypeTitle + "单";
        this.BatchPrintData.push(this.printData);
        this.$store.dispatch("stockBillCount/setCount", this.BatchPrintData.length)
        this.$emit("batchPrint", this.BatchPrintData)
      },
      /** 部门数据回显 */
      undataAddSee(id) {
        return new Promise((resolve, reject) => {
          let parentArr = [], //用于储存父类部门信息
            childrenArr = []; ////用于储存子类部门信息
          getEcho(id, this.optionsworkDept, parentArr, childrenArr).then(() => {
            setTimeout(() => {
              for (let i = 0; i < res.length; i++) {
                if (success.indexOf(res[i]) === -1) {
                  success.push(res[i])
                }
              }
              let getNode = JSON.parse(JSON.stringify(success)) //拷贝vuex信息，避免影响源数据
              getNode.forEach(v => {
                if (v.ancestors.indexOf(",") > -1) { //是否是子部门
                  v.ancestors = v.ancestors.split(",").length; //拼接数组长度，用于排序
                } else {
                  v.ancestors = 1;
                }
              })
              getNode.sort((a, b) => { //排序得到正常的部门顺序
                return a.ancestors - b.ancestors
              })
              this.selectworkDept = [];
              this.printWorkDept = [];
              getNode.forEach(code => {
                this.selectworkDept.push(code.deptId);
                this.printWorkDept.push(code.deptName)
              })
            }, 0)

          });
          resolve()
        })
      },
    },

    filters: {
      getInvoice(status) {
        switch (status) {
          case '0':
            return "开立"
            break;
          case '1':
            return "审核"
            break;
          case '2':
            return "退回"
            break;
          default:
            return "无"
        }
      }
    },

    watch: {

    }
  };
</script>

<style lang="scss">
  .add-purchas {
    .code-red {
      input {
        color: #fc6a6a !important;
      }
    }

    .code-yellow {
      input {
        color: #e6a23c !important;
      }
    }

    .purchas-information {
      .el-card__body {
        padding: 15px 20px 10px 20px;
      }
    }

    .set-location {
      display: flex;
      align-items: center;

      .location-btn {
        flex-shrink: 1;
        width: 40px;
        margin-right: 5px;
        height: 20px;
      }
    }

    .el-card.is-always-shadow {
      overflow: inherit;
    }

    .blue-red {
      label:nth-of-type(2) {
        .is-checked .el-radio__inner {
          background: #fc6a6a;
          border-color: #fc6a6a;
        }

        .is-checked+.el-radio__label {
          color: #fc6a6a;
        }

        font-size: 20px;
      }
    }

    .unfold-way {
      display: flex;
      justify-content: space-between;
      width: 100%;
      align-items: center;

      p {
        margin: 0;
        margin-bottom: 8px;
      }
    }

    .con-spec {
      display: flex;
      text-align: center;
      justify-content: center;
      word-wrap: break-word;

      .spec-l {
        padding-right: 2px;

        input {
          text-align: right !important;

        }
      }

      .spec-r {
        input {
          text-indent: 0 !important;
          text-align: left !important;
        }

      }
    }

    .el-radio__input.is-disabled.is-checked .el-radio__inner {
      border-color: #1890ff;
      background: #1890ff;

    }

    .el-radio__input.is-disabled+span.el-radio__label {
      color: #333;
    }

    .el-input--medium .el-input__inner {
      padding-right: 25px;
    }

    .el-date-editor .el-input__inner {
      padding-right: 10px;
    }

    .info-list {
      display: flex;
      justify-content: space-between;
      align-items: flex-end;

      .articles-numbers {
        margin-bottom: 5px;

        a {
          font-size: 14px;
          margin-left: 15px;
          color: #1890ff;

          &:hover {
            opacity: .7;
          }
        }
      }

      .wn-in-box {
        display: flex;
      }

      .list-box {
        .box-input {
          display: flex;
          margin-right: 30px;
          align-items: center;
          margin-bottom: 5px;
          position: relative;

          .input-select {
            margin-left: 5px;
          }

          p {
            flex-shrink: 0;
            width: 70px;
            font-size: 14px;
            margin: 0px;
          }

          .el-input {
            width: auto;
            flex: 1;
          }

          .el-textarea {
            width: 100%;
            flex: 1;
          }

          .drop-menu {
            position: absolute;
            width: 212px;
            box-shadow: 1px 1px 5px 1px #eee;
            max-height: 500px;
            overflow: auto;
            z-index: 9;
            background: #fff;
            left: 70px;
            top: 35px;
            color: #606266;

            p {
              padding: 10px;
              text-align: center;
              width: 100%;
              color: #606266;
            }

            .menu-close {
              position: sticky;
              top: 0;
              width: 212px;
              text-align: right;
              height: 30px;
              background: #fff;
              line-height: 30px;
              border-bottom: 1px solid #eee;

              .search-top {
                display: flex;
                justify-content: space-between;
                align-items: center;

                span {
                  font-size: 14px;
                  margin-left: 10px;
                  color: #606266;
                }
              }

              a {
                padding-right: 10px;
                font-size: 14px;
              }

            }

            .ma-list {
              .list-t {
                text-align: left;
                cursor: pointer;
                border-bottom: 1px solid #eee;
                font-size: 14px;
                padding-top: 5px;
                padding-bottom: 5px;

                &:hover {
                  background-color: #F5F7FA;
                }

                .po-content {
                  display: flex;
                  align-items: center;
                }

                span {
                  display: inline-block;
                  margin-left: 10px;
                  margin-right: 10px;
                }
              }
            }
          }
        }


      }

      .el-input.is-disabled .el-input__inner {
        color: #333;
      }

      .blue-color .is-disabled .el-input__inner {
        color: #1890ff;
      }
    }

    .purchas-content {
      margin-top: 10px;

      .el-table--border {
        border-left: 1px solid #dfe6ec;
      }

      .el-table__row td:first-child,
      .has-gutter th:first-child {
        border-left: none;
      }

      .clearfix {
        p {
          margin: 0;
          margin-bottom: 8px;
        }
      }

      .el-card__header {
        padding-right: 20px;
      }

      .el-input__inner {
        height: 31px;
        line-height: 0;
        padding: 0 5px;
        color: #606266;
      }

      .el-input__icon {
        line-height: 0;
      }

      .el-table .cell {
        overflow: inherit;
      }

      .select-name {
        display: flex;
        align-items: center;

        .el-input {
          flex: 1;
        }

        span {
          line-height: 20px;
          margin-left: 5px;
          font-size: 14px;
          margin-right: 5px;
        }
      }

      .el-card__body {
        padding-bottom: 10px;
      }

      .table-data {
        .el-table__body-wrapper {
          overflow: auto;

          .cell {
            height: auto !important;
          }
        }

        .el-table .el-table__header-wrapper th,
        .el-table .el-table__fixed-header-wrapper th {
          height: 35px !important;
        }

        .cell {
          padding: 0;
        }

        .el-input--medium .el-input__inner {
          background: none;
          border-color: #fff;
          border-radius: 0;
          padding: 0;
          text-align: center;
        }

        .el-form-item.is-error .el-input__inner,
        .el-form-item.is-error .el-input__inner:focus,
        .el-form-item.is-error .el-textarea__inner,
        .el-form-item.is-error .el-textarea__inner:focus {
          border-color: #ff4949;
        }

        .el-table--enable-row-hover .el-table__body tr:hover>td {
          background: none;

        }

        .el-table--medium th,
        .el-table--medium td {
          padding: 0 !important;
          text-align: center;
        }

        .el-form-item--medium .el-form-item__content {
          line-height: 30px;
        }

        .add-icon {
          margin-top: 5px;
          text-align: center;
        }
      }

      .el-form-item__error {
        padding-top: 0;
        top: 90%;
      }
    }

    .save-btn {
      margin: 20px;

      button {
        width: 200px;
      }
    }

    .el-form-item {
      margin-bottom: 0;

    }

    .button-plan span {
      display: inline-block;

      button {
        margin-left: 10px;
      }
    }

    .invoice-box {
      background-color: #F5F7FA;
      border-color: #dfe4ed;
      width: 100%;
      padding: 0px 15px;
      height: 32px;
      line-height: 27px;
      border: 1px solid #DCDFE6;
      border-radius: 4px;
      margin-bottom: 3px;
      margin-top: 3px;

      span {
        font-size: 14px;
      }
    }

    .text-right {
      input {
        text-align: right !important;
        padding-right: 10px !important;
      }

      span {
        display: inline-block;
        padding-right: 10px;
        width: 100%;
        text-align: right;
      }
    }

    .select-box {
      position: relative;

      .drop-menu1 {
        position: absolute;
        width: 449px;
        box-shadow: 1px 1px 5px 1px #eee;
        max-height: 300px;
        overflow: auto;
        z-index: 9;
        background: #fff;

        .menu-close {
          position: sticky;
          top: 0;
          width: 449px;
          text-align: right;
          height: 30px;
          background: #fff;
          line-height: 30px;

          .search-top {
            display: flex;
            justify-content: space-between;
            align-items: center;

            span {
              font-size: 14px;
              margin-left: 10px;
            }
          }

          a {
            padding-right: 10px;
          }

        }

        .ma-list {
          margin-bottom: 10px;

          .list-t {
            text-align: left;
            cursor: pointer;
            border-bottom: 1px solid #eee;
            font-size: 14px;

            &:hover {
              background-color: #F5F7FA;
            }

            span {
              display: inline-block;
              margin-left: 10px;
              margin-right: 10px;
            }
          }
        }
      }
    }

    .matrial-box .basicfile-box .cell {
      height: auto !important
    }

    .el-card__header {
      padding-right: 20px;
    }
  }
</style>
